require(['jquery','knockout','bootstrap', 'uui', 'tree', 'ip'], function ($, ko) {
	window.ko = ko;
	var viewModel = {
		//表格数据模型
		userDataTable: new u.DataTable({
			meta: {
				'uid' : {type: 'string'},
				'loginname': {type: 'string'},
				'email': {type: 'string'},
				'status': {type: 'string'},
				'sex' : {type: 'string'},
				'phone' : {type: 'string'},
				'address' : {type: 'string'},
				'addTime' : {type: 'string'},
				'level' : {type: 'string'},
				'loginpass' : {type: 'string'},
				'number' : {type: 'string'},
			}
		})
	};
	
	showUser = function() {
		var name = $('#search-name').val();
		var addTime = $('#search-addTime').val();
		$.ajax({
			type : "GET",
			url : "/bookstore/my/findUser",
			data : {
				"name" : encodeURI(name),
				"addTime" : addTime
			},
			dataType : 'json',
			success : function(result) {
				viewModel.userDataTable.clear();
				var data = result.data;
				console.log(data);
				if (data) { //数据不为空，对显示数据进行处理
					for (var i = 0; i < data.length; i++) {
						data[i].number = i + 1;
						if (data[i].sex == "1") {
							data[i].sex = "男";
						} else if (data[i].sex == "0"){
							data[i].sex = "女";
						} else {
							data[i].sex ="保密";
						}
					}
				}
				viewModel.userDataTable.setSimpleData(data);
				layer.msg('查询成功!',{icon:1,time:1000});
				var userData = viewModel.userDataTable.getSimpleData();
				for (var j = 0; j < userData.length ; j ++) {
					if (userData[j].status == true) {
						$(".td-status").eq(j).html('<span class="label label-success radius">已启用</span>');
						$(".startOrStop").eq(j).addClass("btn btn-xs btn-success");
					} else {
						$(".td-status").eq(j).html('<span class="label label-defaunt radius">已停用</span>');
						$(".startOrStop").eq(j).addClass("btn btn-xs");
					}
				}
			},
			error : function() {
				layer.msg('查询失败!',{icon:2,time:1000});
			}
		})
	};
	
	/*用户-停用*/
	member_stop = function(obj){
		layer.confirm('确认要停用吗？',function(index){
			$(obj).parents("tr").find(".td-manage").prepend('<a style="text-decoration:none" class="btn btn-xs " onClick="member_start(this)" href="javascript:;" title="启用"><i class="icon-ok bigger-120"></i></a>');
			$(obj).parents("tr").find(".td-status").html('<span class="label label-defaunt radius">已停用</span>');
			$(obj).remove();
			layer.msg('已停用!',{icon: 5,time:1000});
		});
	}

	/*用户-启用*/
	member_start = function(obj){
		layer.confirm('确认要启用吗？',function(index){
			$(obj).parents("tr").find(".td-manage").prepend('<a style="text-decoration:none" class="btn btn-xs btn-success" onClick="member_stop(this)" href="javascript:;" title="停用"><i class="icon-ok bigger-120"></i></a>');
			$(obj).parents("tr").find(".td-status").html('<span class="label label-success radius">已启用</span>');
			$(obj).remove();
			layer.msg('已启用!',{icon: 6,time:1000});
		});
	}
	
	/*用户-新增*/
	viewModel.addUser = function() {
		//清空数据
		$('#edit-loginname').val('');
		$('#edit-realname').val('');
		$('#edit-phone').val('');
		$('#edit-mobile').val('');
		$('#edit-email').val('');
		$('#edit-address').val('');
		$('#edit-loginpass').val('');
		layer.open({
	        type: 1,
	        title: '添加用户',
			maxmin: true, 
			shadeClose:false, //点击遮罩关闭层
	        area : ['800px' , ''],
	        content:$('#add_menber_style'),
			btn:['提交','取消'],
			yes:function(index,layero) {	
			 var num=0;
			 var str="";
			 $(".add_menber input[type$='text']").each(function(n){
		          if($(this).val()=="") {
					   layer.alert(str+=""+$(this).attr("name")+"不能为空！\r\n",{
		                title: '提示框',				
						icon:0,								
		          }); 
				    num++;
		            return false;            
		          } 
			 });
			if (num > 0) {
				return false;
			}
			//获取数据
			loginname = $('#edit-loginname').val();
			phone = $('#edit-phone').val();
			email = $('#edit-email').val();
			address = $('#edit-address').val();
			var statusObjs = $("[name = status-radio]");
			var sexObjs = $("[name = sex-radio]");
			if (statusObjs[0].checked) {
				status = true;
			} else {
				status = false;
			}
			if (sexObjs[0].checked) {
				sex = 1;
			}
			if (sexObjs[1].checked) {
				sex = 0;
			}
			if (sexObjs[2].checked) {
				sex = 2;
			}
			loginpass = $('#edit-loginpass').val();
			$.ajax({
				type : "POST",
				url : "/bookstore/my/addUser",
					data : {
						"loginname" : loginname,
						"phone" : phone,
						"email" : email,
						"address" : address,
						"status" : status,
						"sex" : sex,
						"loginpass" : loginpass
					},
					dataType : 'json',
					success : function(result) {
					showUser();
					var data = result.data;
					layer.alert('添加成功！',{
			               title: '提示框',				
						   icon:1,		
						  });
				    layer.close(index);
				},
				error : function() {
					layer.alert('添加失败！',{
			               title: '提示框',				
						   icon:2,		
						  });
					return false;
				}
			});
		  }
	    });
	};
	
	/*用户-编辑*/
	viewModel.member_edit = function(data) {
		var loginname = data.data.loginname.value;
		var phone = data.data.phone.value;
		var email = data.data.email.value;
		var address = data.data.address.value;
		var uid = data.data.uid.value;
		var loginpass = "";
		var sex = "";
		var status = "";
		//填充数据
		$('#edit-loginname').val(loginname);
		$('#edit-realname').val(loginname);
		$('#edit-phone').val(phone);
		$('#edit-mobile').val(phone);
		$('#edit-email').val(email);
		$('#edit-address').val(address);
		$('#edit-loginpass').val('默认不可显示');
		if (data.data.sex.value == "女") {
			$('#edit-girl').prop("checked", true);
		} else if (data.data.sex.value == "男") {
			$('#edit-boy').prop("checked", true);
		} else {
			$('#edit-other').prop("checked", true);
		}
		if (data.data.status.value == true) {
			$('#edit-start').prop("checked", true);
		} else {
			$('#edit-stop').prop("checked", true);
		}
		layer.open({
	        type: 1,
	        title: '修改用户信息',
			maxmin: true, 
			shadeClose:false, //点击遮罩关闭层
	        area : ['800px' , ''],
	        content:$('#add_menber_style'),
			btn:['提交','取消'],
			yes:function(index,layero) {	
			 var num=0;
			 var str="";
			 $(".add_menber input[type$='text']").each(function(n){
		          if($(this).val()=="") {
					   layer.alert(str+=""+$(this).attr("name")+"不能为空！\r\n",{
		                title: '提示框',				
						icon:0,								
		          }); 
				    num++;
		            return false;            
		          } 
			 });
			if (num > 0) {
				return false;
			}
			//获取数据
			loginname = $('#edit-loginname').val();
			phone = $('#edit-phone').val();
			email = $('#edit-email').val();
			address = $('#edit-address').val();
			var statusObjs = $("[name = status-radio]");
			var sexObjs = $("[name = sex-radio]");
			if (statusObjs[0].checked) {
				status = true;
			} else {
				status = false;
			}
			if (sexObjs[0].checked) {
				sex = 1;
			}
			if (sexObjs[1].checked) {
				sex = 0;
			}
			if (sexObjs[2].checked) {
				sex = 2;
			}
			if ($('#edit-loginpass').val() == "默认不可显示") {
				loginpass = "";
			} else {
				loginpass = $('#edit-loginpass').val();
			}
			$.ajax({
				type : "POST",
				url : "/bookstore/my/editUser",
					data : {
						"uid" : uid,
						"loginname" : loginname,
						"phone" : phone,
						"email" : email,
						"address" : address,
						"status" : status,
						"sex" : sex,
						"loginpass" : loginpass
					},
					dataType : 'json',
					success : function(result) {
					showUser();
					var data = result.data;
					layer.alert('添加成功！',{
			               title: '提示框',				
						   icon:1,		
						  });
				    layer.close(index);
				},
				error : function() {
					layer.alert('添加失败！',{
			               title: '提示框',				
						   icon:2,		
						  });
					return false;
				}
			});
		  }
	    });
	};
	
	/*用户-删除*/
	viewModel.member_del = function(data){
		layer.confirm('确认要删除吗？',function(index){
			$.ajax({
				type : "POST",
				url : "/bookstore/my/delUser",
				data : {
					"uid" : data.data.uid.value,
				},
				dataType : 'json',
				success : function(result) {
					showUser();
					layer.msg('已删除!',{icon:1,time:1000});
				},
				error : function() {
					layer.msg('删除失败!',{icon:2,time:1000});
				}
			});
		});
	};
	
	function init(){
		showUser();
		//ko.cleanNode($("#list-page")[0]);
		var app = u.createApp(
				{
					el: '.page-content',
					model: viewModel
				}
		);
    };
    

    init();

});